﻿//**********************************************************************************
// Author: Michael Ceranski
// Project: http://weblog.codeplex.com
// Date: 6/26/2010
// Description: Toggles the visibility of the widgets
//**********************************************************************************
using System;
using System.Web.Mvc;

namespace WeBlog
{
    public class WidgetBarAttribute : ActionFilterAttribute
    {
        private bool _visible;
        public static readonly string KEY = "WidgetBarVisibility";

        public WidgetBarAttribute(bool visible)
        {
            _visible = visible;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewData[KEY] = _visible;
        }

    }

    public static class WidgetBarHelpers
    {
        public static bool WidgetBarVisible(this HtmlHelper html)
        {
            bool visible;

            if (html.ViewData.ContainsKey(WidgetBarAttribute.KEY) == false ) return true;

            if (Boolean.TryParse(html.ViewData[WidgetBarAttribute.KEY].ToString(), out visible))
                return visible;
            
            return true;            
        }
    }
}